package mobileann.mafamily.utils;

import mobileann.mafamily.db.model.ClickModuleModel;
import mobileann.mafamily.db.provider.ClickModuleProvider;
import android.app.Activity;
import android.widget.Toast;

import com.mobileann.mafamily.R;
import com.umeng.socialize.bean.RequestType;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.SmsShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

public class ShareUtil {
    private final static UMSocialService mController = UMServiceFactory
	    .getUMSocialService("com.umeng.share", RequestType.SOCIAL);
    private static ShareUtil instance;

    public static final String WECHAT_APPID = "wx6583ba84722db160";
    public static final String WECHAT_APPSECRET = "f87ef67287d2897107ff2c653a760310";
    public static final String QQ_APPID = "101027754";
    public static final String QQ_APPSECRET = "d3174adc5845085ee61bbdddffc0b9fd";

    public static final String[] TEXTS = { "QQ好友", "微信好友", "QQ空间", "微信朋友圈",
	    "新浪微博", "短信" };
    public static final int[] ICONS = { R.drawable.login_qq,
	    R.drawable.login_weixin, R.drawable.icon_share_qzone,
	    R.drawable.icon_share_wxzone, R.drawable.login_weibo,
	    R.drawable.icon_share_sms };
    private static final int TAG_SHARE_TO_QQ = 10;
    private static final int TAG_SHARE_TO_WECHAT = 11;
    private static final int TAG_SHARE_TO_QZONE = 12;
    private static final int TAG_SHARE_TO_WCIRCLE = 13;
    private static final int TAG_SHARE_TO_SINA = 14;
    private static final int TAG_SHARE_TO_SMS = 15;

    public static final String SHARE_TITLE = "跟屁虫--爱Ta就时刻关注Ta";
//    public static final String SHARE_CONTENT = "随时了解对方位置，督促对方健康使用手机";
    public static final String SHARE_URL = "http://wap.mobileann.com";
    // public static final String SHARE_URL = "http://t.cn/RZjOT15";
//    public static final String SHARE_CONTENT2 = "推荐一款手机软件，叫做跟屁虫，能定位家人位置，还能保护视力。点击下面的链接下载试试吧";
    public static final String SHARE_CONTENT3 = "给你推荐一款手机软件，叫做跟屁虫，能定位家人位置，还能保护视力。点击下面的链接下载试试吧";
    public static final String SHARE_CONTENT4 = "记得安装注册后填写推荐码：";
    public static final String SHARE_CONTENT5 = "，领取金币";
    public static final String SHARE_CONTENT6 = "注册凭推荐码";
    public static final String SHARE_CONTENT7 = "赢取金币，好玩又好用";
    public static final String SHARE_CONTENT8 = "注册凭推荐码"+SPUtils.getRefereeCode(SPUtils.getMySelf(SPUtils.MYUID))+"赢取金币，好玩又好用";
    public synchronized static ShareUtil getInstance() {
	if (instance == null) {
	    instance = new ShareUtil();
	}
	return instance;
    }

    private void closeToast() {
	mController.getConfig().closeToast();
    }
    /**
     * 设置新浪微博分享
     */
    public void setWeibo(Activity act) {
	// 添加新浪SSO授权
	mController.getConfig().setSsoHandler(new SinaSsoHandler());
	closeToast();
	if (NetUtils.getInstance().netstate() != 0) {
	    setSinaShareContent(act);
	    performShare(act, SHARE_MEDIA.SINA, ShareUtil.TAG_SHARE_TO_SINA);
	} else {
	    Toast.makeText(act, "请检查网络状况", Toast.LENGTH_SHORT).show();
	}
    }

    /**
     * 设置新浪分享内容
     */
    private void setSinaShareContent(Activity act) {
	SinaShareContent sinaContent = new SinaShareContent();
	sinaContent.setTitle(ShareUtil.SHARE_TITLE);
//	sinaContent.setShareContent(SHARE_URL);
	sinaContent.setShareContent(ShareUtil.SHARE_CONTENT3 + ShareUtil.SHARE_URL+ShareUtil.SHARE_CONTENT4+SPUtils.getRefereeCode(SPUtils.getMySelf(SPUtils.MYUID))+ShareUtil.SHARE_CONTENT5);
	// sinaContent.setTargetUrl(SHARE_URL);
	UMImage urlImage = new UMImage(act, R.drawable.img_share_to_sina);
	sinaContent.setShareImage(urlImage);
	mController.setShareMedia(sinaContent);
    }
    /**
     * 设置微信分享
     */
    public void setWeChat(Activity act) {
	// 添加微信平台
	UMWXHandler wxHandler = new UMWXHandler(act, WECHAT_APPID,
		WECHAT_APPSECRET);
	wxHandler.addToSocialSDK();
	closeToast();
	if (wxHandler.isClientInstalled()) {
	    if (NetUtils.getInstance().netstate() != 0) {
		setWeChatShareContent(act);
		performShare(act, SHARE_MEDIA.WEIXIN, TAG_SHARE_TO_WECHAT);
	    } else {
		Toast.makeText(act, "请检查网络状况", Toast.LENGTH_SHORT).show();
	    }
	} else {
	    Toast.makeText(act, "请先安装微信客户端", Toast.LENGTH_SHORT).show();
	}
    }

    /**
     * 设置微信分享内容
     */
    private void setWeChatShareContent(Activity act) {
	WeiXinShareContent weixinContent = new WeiXinShareContent();
	weixinContent.setTitle(SHARE_TITLE);
//	weixinContent.setShareContent(SHARE_CONTENT);
	weixinContent.setShareContent(SHARE_CONTENT6+SPUtils.getRefereeCode(SPUtils.getMySelf(SPUtils.MYUID))+SHARE_CONTENT7);
	weixinContent.setTargetUrl(SHARE_URL);
	UMImage urlImage = new UMImage(act, R.drawable.icon_share_to_friends);
	weixinContent.setShareImage(urlImage);
	mController.setShareMedia(weixinContent);
    }

    /**
     * 设置微信朋友圈分享
     */
    public void setWeChatCircle(Activity act) {
	// 支持微信朋友圈
	UMWXHandler wxCircleHandler = new UMWXHandler(act, WECHAT_APPID,
		WECHAT_APPSECRET);
	wxCircleHandler.setToCircle(true);
	wxCircleHandler.addToSocialSDK();
	closeToast();
	if (wxCircleHandler.isClientInstalled()) {
	    if (NetUtils.getInstance().netstate() != 0) {
		setCircleShareContent(act);
		performShare(act, SHARE_MEDIA.WEIXIN_CIRCLE,
			TAG_SHARE_TO_WCIRCLE);
	    } else {
		Toast.makeText(act, "请检查网络状况", Toast.LENGTH_SHORT).show();
	    }
	} else {
	    Toast.makeText(act, "请先安装微信客户端", Toast.LENGTH_SHORT).show();
	}
    }

    /**
     * 设置朋友圈分享内容
     */
    private void setCircleShareContent(Activity act) {
	CircleShareContent circleMedia = new CircleShareContent();
//	circleMedia.setTitle(SHARE_TITLE + "  " + SHARE_CONTENT);
//	circleMedia.setShareContent(SHARE_CONTENT);
	circleMedia.setTitle(SHARE_TITLE+"\n"+SHARE_CONTENT8);
	circleMedia.setShareContent(SHARE_CONTENT8);
	UMImage urlImage = new UMImage(act, R.drawable.icon_share_to_circle);
	circleMedia.setShareImage(urlImage);
	circleMedia.setTargetUrl(SHARE_URL);
	mController.setShareMedia(circleMedia);
    }

    /**
     * 设置QQ分享
     */
    public void setQQ(Activity act) {
	// 添加QQ支持
	UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(act, QQ_APPID,
		QQ_APPSECRET);
	qqSsoHandler.addToSocialSDK();
	closeToast();
	if (qqSsoHandler.isClientInstalled()) {
	    if (NetUtils.getInstance().netstate() != 0) {
		setQQShareContent(act);
		performShare(act, SHARE_MEDIA.QQ, TAG_SHARE_TO_QQ);
	    } else {
		Toast.makeText(act, "请检查网络状况", Toast.LENGTH_SHORT).show();
	    }
	} else {
	    Toast.makeText(act, "请先安装QQ客户端", Toast.LENGTH_SHORT).show();
	}
    }

    /**
     * 设置QQ分享内容
     */
    private void setQQShareContent(Activity act) {
	QQShareContent qqShareContent = new QQShareContent();
	qqShareContent.setTitle(SHARE_TITLE);
//	qqShareContent.setShareContent(SHARE_CONTENT);
	qqShareContent.setShareContent(SHARE_CONTENT6+SPUtils.getRefereeCode(SPUtils.getMySelf(SPUtils.MYUID))+SHARE_CONTENT7);
	qqShareContent.setTargetUrl(SHARE_URL);
	UMImage urlImage = new UMImage(act, R.drawable.icon_share_to_friends);
	qqShareContent.setShareImage(urlImage);
	mController.setShareMedia(qqShareContent);
	
    }

    /**
     * 设置QQ空间分享
     */
    public void setQQZone(Activity act) {
	// 添加QZone平台
	QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(act, QQ_APPID,
		QQ_APPSECRET);
	qZoneSsoHandler.addToSocialSDK();
	closeToast();
	if (qZoneSsoHandler.isClientInstalled()) {
	    if (NetUtils.getInstance().netstate() != 0) {
		setQZoneShareContent(act);
		performShare(act, SHARE_MEDIA.QZONE, TAG_SHARE_TO_QZONE);
	    } else {
		Toast.makeText(act, "请检查网络状况", Toast.LENGTH_SHORT).show();
	    }
	} else {
	    Toast.makeText(act, "请先安装QQ客户端", Toast.LENGTH_SHORT).show();
	}
    }

    /**
     * 设置QQ空间分享内容
     */
    private void setQZoneShareContent(Activity act) {
	QZoneShareContent qzone = new QZoneShareContent();
	qzone.setTitle(SHARE_TITLE);
//	qzone.setShareContent(SHARE_CONTENT);
	qzone.setShareContent(SHARE_CONTENT6+SPUtils.getRefereeCode(SPUtils.getMySelf(SPUtils.MYUID))+SHARE_CONTENT7);
	qzone.setTargetUrl(SHARE_URL);
	UMImage urlImage = new UMImage(act, R.drawable.icon_share_to_qzone);
	qzone.setShareImage(urlImage);
	mController.setShareMedia(qzone);
    }

    

    /**
     * 设置短信分享
     */
    public void setSMS(Activity act) {
	// 添加短信平台
	SmsHandler smsHandler = new SmsHandler();
	smsHandler.addToSocialSDK();
	closeToast();
	setSmsShareContent(act);
	performShare(act, SHARE_MEDIA.SMS, TAG_SHARE_TO_SMS);
    }

    /**
     * 设置短信分享内容
     */
    private void setSmsShareContent(Activity act) {
	SmsShareContent sms = new SmsShareContent();
//	sms.setShareContent("给你" + SHARE_CONTENT2 + SHARE_URL);
	sms.setShareContent( SHARE_CONTENT3 + SHARE_URL+SHARE_CONTENT4+SPUtils.getRefereeCode(SPUtils.getMySelf(SPUtils.MYUID))+SHARE_CONTENT5);
	mController.setShareMedia(sms);
    }

    public void performShare(final Activity act, SHARE_MEDIA platform,
	    final int tag) {
	mController.postShare(act, platform, new SnsPostListener() {
	    @Override
	    public void onStart() {
	    }

	    @Override
	    public void onComplete(SHARE_MEDIA platform, int eCode,
		    SocializeEntity entity) {
		if (eCode == StatusCode.ST_CODE_SUCCESSED) {
		    // 分享成功
		    ClickModuleModel model = new ClickModuleModel(act);
		    switch (tag) {
		    case TAG_SHARE_TO_QQ:
			if (SPUtils.getIsFirstQQShare(SPUtils.getUID())) {
			    // model.updateClickTimes(
			    // ClickModuleProvider.SHAREQQ_TIMES,
			    // SPUtils.getUID2(), 1);
			    model.updateClickTimes(
				    ClickModuleProvider.SHAREQQ_TIMES,
				    SPUtils.getMySelf(SPUtils.MYUID), 1);
			    SPUtils.setIsFirstQQShare(SPUtils.getUID(), false);
			}
			break;
		    case TAG_SHARE_TO_WECHAT:
			if (SPUtils.getIsFirstMicroShare(SPUtils.getUID())) {
			    // model.updateClickTimes(
			    // ClickModuleProvider.SHAREMICRO_TIMES,
			    // SPUtils.getUID2(), 1);
			    model.updateClickTimes(
				    ClickModuleProvider.SHAREMICRO_TIMES,
				    SPUtils.getMySelf(SPUtils.MYUID), 1);
			    SPUtils.setIsFirstMicroShare(SPUtils.getUID(),
				    false);
			}
			break;
		    case TAG_SHARE_TO_QZONE:
			if (SPUtils.getIsFirstQZoneShare(SPUtils.getUID())) {
			    // model.updateClickTimes(
			    // ClickModuleProvider.SHAREQZONE_TIMES,
			    // SPUtils.getUID2(), 1);
			    model.updateClickTimes(
				    ClickModuleProvider.SHAREQZONE_TIMES,
				    SPUtils.getMySelf(SPUtils.MYUID), 1);
			    SPUtils.setIsFirstQZoneShare(SPUtils.getUID(),
				    false);
			}
			break;
		    case TAG_SHARE_TO_WCIRCLE:
			if (SPUtils.getIsFirstFriendShare(SPUtils.getUID())) {
			    // model.updateClickTimes(
			    // ClickModuleProvider.SHAREFRIENDS_TIMES,
			    // SPUtils.getUID2(), 1);
			    model.updateClickTimes(
				    ClickModuleProvider.SHAREFRIENDS_TIMES,
				    SPUtils.getMySelf(SPUtils.MYUID), 1);
			    SPUtils.setIsFirstFriendShare(SPUtils.getUID(),
				    false);
			}
			break;
		    case TAG_SHARE_TO_SINA:
			if (SPUtils.getIsFirstWebShare(SPUtils.getUID())) {
			    // model.updateClickTimes(
			    // ClickModuleProvider.SHAREWEB_TIMES,
			    // SPUtils.getUID2(), 1);
			    model.updateClickTimes(
				    ClickModuleProvider.SHAREWEB_TIMES,
				    SPUtils.getMySelf(SPUtils.MYUID), 1);
			    SPUtils.setIsFirstWebShare(SPUtils.getUID(), false);
			}
			break;
		    case TAG_SHARE_TO_SMS:
			if (SPUtils.getIsFirstSMSShare(SPUtils.getUID())) {
			    // model.updateClickTimes(
			    // ClickModuleProvider.SHARESHORTMSG_TIMES,
			    // SPUtils.getUID2(), 1);
			    model.updateClickTimes(
				    ClickModuleProvider.SHARESHORTMSG_TIMES,
				    SPUtils.getMySelf(SPUtils.MYUID), 1);
			    SPUtils.setIsFirstSMSShare(SPUtils.getUID(), false);
			}
			break;
		    default:
			break;
		    }
		    MyTaskUtils.getInstance().doClickMedule2Server(act);
		} else {
		    // 分享失败
		}
	    }
	});
    }
}
